<template>
  <a-modal
    title="客户"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.cCusName" placeholder="客户名称" />

                <!-- <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="cCusName">客户名称</a-select-option>

                <a-select-option key="CustomerKCode">客户级别</a-select-option>
              </a-select> -->
              </a-form-item>
            </a-col>

            <!-- <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword[0]" placeholder="关键字" />
            </a-form-item>
          </a-col> -->

            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
                >查询</a-button
              >
              <a-button style="margin-left: 8px" @click="Repossess">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :scroll="{ y: scroll }"
        :columns="columns"
        :rowKey="(row, index) => row.cCusCode"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, getCheckboxProps: CheckboxProps }"
        :bordered="true"
        size="small"
      >
        <template slot="AllocationStatus" slot-scope="text, record">
          <a-tag v-if="record.AllocationStatus !== 1" @click="Distribute([record.cCusCode])" color="red">待分配</a-tag>
          <a-tag v-else-if="record.AllocationStatus === 1" @click="Cancel(record)" color="green">已分配</a-tag>
        </template>
        <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
        <span slot="action" slot-scope="text, record">
          <template>
            <!-- <a v-if="record.AllocationStatus === 0" @click="Distribute([record.cCusCode])">分配</a>
          <a-divider v-if="record.AllocationStatus === 0" type="vertical" />
          <a v-if="record.AllocationStatus === 1" @click="Cancel(record)">解除</a>
          <a-divider v-if="record.AllocationStatus === 1" type="vertical" /> -->
            <a v-if="hasPerm('Cust_CustomerInfoFormal.Edit')" @click="handleEdit(record.cCusCode)">详情</a>
            <a-divider
              v-if="hasPerm('Cust_CustomerInfoFormal.Edit') && hasPerm('Cust_CustomerInfoFormal.Delete')"
              type="vertical"
            />

            <a @click="contactPerson(record)">联系人</a>
            <a-divider type="vertical" />

            <a @click="contactPerson(record)">地址簿</a>
          </template>
        </span>
      </a-table>
    </a-card>
  </a-modal>
</template>

<script>
//客户分配

import moment from 'moment'
const columns = [
  { title: '客户编码', dataIndex: 'cCusCode', width: '10%' },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left', width: '17%' },

  { title: '地址', dataIndex: 'cCusAddress', ellipsis: true },
  //  { title: '联系人', dataIndex: 'cCusPerson', width: '5%' },

  {
    title: '销售经理',
    dataIndex: 'AllocationName',
    customRender: (value, row) => {
      if (row.AllocationsList.length > 0) {
        return row.AllocationsList.map(item => item.SalesmanName).join(',')
      } else {
        return ''
      }
    }
  }
]

export default {
  computed: {
    scroll() {
      return Number(document.documentElement.clientHeight) * 0.45
    }
  },
  mounted() {
    this.getlist()
    this.getDataList()
  },
  data() {
    return {
      visible: false,
      IsFormal: true,
      data: [],
      listBind: [],
      list: [], //省级
      listChildren: [], //市
      county: [], //县
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'cCusCode', order: 'desc' },
      // queryParam: {},
      queryParam: {},
      loading: false,
      columns,
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    //省市区的模糊查询
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    CheckboxProps(record) {
      let a = this.listBind.map(item => {
        return item.cCusName
      })

      var cCusName = a.includes(record.cCusName)

      return {
        props: {
          disabled: cCusName
        }
      }
    },
    Repossess() {
      this.queryParam = {}
      this.getDataList()
    },

    //市级change
    city(e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=' + e, {}).then(resJson => {
        //console.log('res', resJson)
        this.loading = false

        delete this.queryParam[3].keyword
        delete this.queryParam[4].keyword
        this.listChildren = resJson.Data
      })
    },

    //县change
    village(e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=' + e, {}).then(resJson => {
        this.loading = false

        this.county = resJson.Data
      })
    },
    //获取省份信息
    getlist(e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=0', {}).then(resJson => {
        this.loading = false
        this.list = resJson.Data
      })
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    Distribute(row) {
      this.IsFormal = true
      this.$refs.MyCustomer.openForm(row, '分配客户')

      //console.log(row)
    },
    getDataList() {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      //console.log(selectedRowKeys, selectedRows)
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    contactPerson(row) {
      //console.log(row)

      this.$refs.List.openForm(row, '客户联系人')
    },

    handleEdit(id) {
      this.$refs.details.openForm(id, '客户详情')
    },
    handleChoose() {
      this.$emit('OnUserBind', this.selectedRows)
      this.visible = false
    },
    openForm(data) {
      this.visible = true

      this.listBind = data

      this.queryParam = {}
      this.selectedRowKeys = []
      this.pagination.current = 1
      this.pagination.pageSize = 25
      this.getDataList()
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
